//
//  YDCommonSettingsController.m
//  CRM
//
//  Created by ios on 16/9/27.
//  Copyright © 2016年 YD_iOS. All rights reserved.
//

#import "YDCommonSettingsController.h"
#import "ViewController.h"
#import "YDHelpFeedbackController.h"
#import "YDAboutMyController.h"

#import "YDUserInfoCell.h"


@interface YDCommonSettingsController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIButton *exitLoginButton; //退出登录按钮

@property (nonatomic, copy) NSString *currentVersion;

@property (nonatomic, copy) NSString *bestNewVersion;

@property (nonatomic, strong) UILabel *versionLabel; //发现新版本的红圈圈

@end

@implementation YDCommonSettingsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    
    [self createView];
    
    [self getVersionNumber];
}

#pragma mark － ================界面===================
- (void)createView
{
    self.title = @"通用设置";

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64.0f) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = kViewControllerBackgroundColor;
    [_tableView registerNib(@"YDUserInfoCell")];
    [self.view addSubview:_tableView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15.0f)];
    headerView.backgroundColor = kViewControllerBackgroundColor;
    _tableView.tableHeaderView = headerView;
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 74.0f)];
    footerView.backgroundColor = kViewControllerBackgroundColor;
    _tableView.tableFooterView = footerView;
    
    //退出登录按钮
    _exitLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _exitLoginButton.frame = CGRectMake(15.0f, 15.0f, kScreenWidth - 30.0f, 44.0f);
    [_exitLoginButton setTitle:@"退出登录" forState:UIControlStateNormal];
    _exitLoginButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    _exitLoginButton.backgroundColor = [UIColor whiteColor];
    _exitLoginButton.layer.cornerRadius = 5.0f;
    _exitLoginButton.clipsToBounds = YES;
    [_exitLoginButton setTitleColor:[UIColor colorWithHexString:@"f98089"] forState:UIControlStateNormal];
    [_exitLoginButton addTarget:self action:@selector(exitLogin) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:_exitLoginButton];

}

#pragma mark 退出登录
- (void)exitLogin
{
    [[[UIAlertView alloc] initWithTitle:@"是否确定退出登录？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        //删除数据
        [kUserDefaults removeObjectForKey:kAPP_SID];
        
        UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ViewController *vc = [main instantiateViewControllerWithIdentifier:@"ViewController"];
        [self presentViewController:vc animated:YES completion:^{
            self.view.window.rootViewController = vc;
        }];
    }
}

#pragma mark 获取版本号  
- (void)getVersionNumber
{
    //最新版本号
    //192.168.8.118:8080/admin-webapp/version/getVersion?sid=xxxxx
    NSString *urlString = [NSString stringWithFormat:@"%@sid=%@", kGetVersionAddress, getNSUser(kAPP_SID)];
    [YDDataService POST:@{} url:urlString block:^(id result) {
        
        NSDictionary *iOSInfo = [result valueForKey:@"iOS_CRM"];
        _bestNewVersion = iOSInfo[@"version"];
        [self.tableView reloadData];
    } failBlock:^(id error) {
        [self.tableView reloadData];
    }];
    
    //当前版本
    _currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    
}

#pragma mark - ================tableView代理方法===================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YDUserInfoCell"];
    
    cell.titleLabel.textColor = [UIColor colorWithHexString:@"354052"];
    cell.subTitleLabel.text = @"";
    
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"版本更新";
        cell.lineView.hidden = NO;
    } else if (indexPath.row == 1) {
        cell.titleLabel.text = @"意见反馈";
        cell.lineView.hidden = NO;
    } else if (indexPath.row == 2) {
        cell.titleLabel.text = @"关于我们";
        cell.lineView.hidden = YES;
    }
    
    if (indexPath.row == 0) {
        
        [cell addSubview:self.versionLabel];
        
        if ([_bestNewVersion isEqualToString:_currentVersion]) {
            //最新版本
            self.versionLabel.hidden = YES;
        } else {
            //发现新版本
            self.versionLabel.hidden = NO;
        }
        
        if (_bestNewVersion == nil) {
            self.versionLabel.hidden = YES;
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        if ([_bestNewVersion isEqualToString:_currentVersion]) {
            [MBProgressHUD showTips:@"已是最新版本" toView:kWindow];
        } else {
//            YDVersionUpdateController *versionUpdateVC = [[YDVersionUpdateController alloc] init];
//            [self.navigationController pushViewController:versionUpdateVC animated:YES];
        }
        
    } else if (indexPath.row == 1) {
        YDHelpFeedbackController *helpFeedbackVC = [[YDHelpFeedbackController alloc] initWithNibName:@"YDHelpFeedbackController" bundle:nil];
        [self.navigationController pushViewController:helpFeedbackVC animated:YES];
    } else if (indexPath.row == 2) {
       
        YDAboutMyController *aboutVC = [[YDAboutMyController alloc] initWithNibName:@"YDAboutMyController" bundle:nil];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
}

- (UILabel *)versionLabel
{
    if (_versionLabel == nil) {
        _versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(73.0f, 10.0f, 13.0f, 13.0f)];
        _versionLabel.backgroundColor = [UIColor redColor];
        _versionLabel.text = @"1";
        _versionLabel.textColor = [UIColor whiteColor];
        _versionLabel.textAlignment = NSTextAlignmentCenter;
        _versionLabel.font = [UIFont systemFontOfSize:12.0f];
        _versionLabel.layer.cornerRadius = 6.5f;
        _versionLabel.layer.masksToBounds = YES;
    }
    return _versionLabel;
}

@end
